package cn.edu.gcu.ssl.system.service;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.gcu.ssl.system.dao.ConfigMessageDao;
import cn.edu.gcu.ssl.system.service.impl.ISendMessage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



@Service("sendMessagSerice")
public class SendMessageService implements ISendMessage{

	@Autowired
	private ConfigMessageDao configmsgdao;

	@Override
	public void sendMessage(List<String> toUserXTIDs, String type, JSONObject msg) {
		// TODO Auto-generated method stub
		List<String>list=new ArrayList<String>(new HashSet<String>(toUserXTIDs));
		JSONObject sendjob=new JSONObject();
		sendjob.put("from",getFrom());
		sendjob.put("to", getTo(list));
		sendjob.put("type",type);
		sendjob.put("msg", msg);
		DefaultHttpClient client=new DefaultHttpClient();
	try {	
		HttpPost post=new HttpPost(configmsgdao.getPubsendurl());
		StringEntity enity=new StringEntity(sendjob.toString(),"UTF-8");
		enity.setContentEncoding("UTF-8");
		enity.setContentType("application/json");
		post.setEntity(enity);
		
			HttpResponse response=client.execute(post);
		} catch (ClientProtocolException e) {
			System.out.println("发送消息出错"+e.toString());
		} catch (IOException e) {
			System.out.println("发送消息出错"+e.toString());
		}finally {
			client.getConnectionManager().shutdown();
		}
		
		
	}

	@Override
	public void sendMessage(String toUserXTID, String type, JSONObject msg) {
		// TODO Auto-generated method stub
	
	}
	
	
	
	//to为json数组,user属性也是json数组
	public JSONArray getTo(List<String>userIds)
	{
		
		JSONObject jto=new JSONObject();
		JSONArray ja=new JSONArray();
		jto.put("no", configmsgdao.getEid());
		jto.put("user",JSONArray.fromObject(userIds));
		ja.add(jto);
		return ja;
	
	}
	
	
	
	
	
	
	
	public JSONObject  getFrom()
	{	
		String time=String.valueOf(System.currentTimeMillis());
		String nonce=String.valueOf(Math.random()*100000);
		JSONObject form=new JSONObject();
		form.put("no", configmsgdao.getEid());
		form.put("pub",configmsgdao.getPubId());
		form.put("time",time);
		form.put("nonce",nonce);
		form.put("pubtoken", getPubtoken(configmsgdao.getEid(),configmsgdao.getPubId(),configmsgdao.getPubKey(),nonce,time));
		return form;
	}
	
	
	
	
	
	
	
	public JSONObject getMsgText(String message) 
	{
		JSONObject jsonMsg=new JSONObject();
		jsonMsg.put("text", message);	
		return jsonMsg;
		
	}
	
	@Override
	public JSONObject getMsgLink(String message,String opentype) {
		// TODO Auto-generated method stub
		JSONObject jsonMsg=new JSONObject();
		jsonMsg.put("url", configmsgdao.getBasePath());
		jsonMsg.put("appid", configmsgdao.getAppid());
		jsonMsg.put("todo",0);
		jsonMsg.put("text", message);	
		jsonMsg.put("opentype", opentype);	
		return jsonMsg;
	}
	
	
	
	

	public String getPubtoken(String... data)
	{	
		Arrays.sort(data);
		return DigestUtils.shaHex(StringUtils.join(data));
	}


	
	
	
	
}
